home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / trim.c < prev    next >
C/C++ Source or Header  |  1993-09-10  |  2KB  |  113 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <string.h>
  20.  
  21. #include "ctyp_dec.h"
  22. #include "rec.h"
  23. #include "window.h"
  24. #include "ed_dec.h"
  25. #include "buffer.h"
  26. #include "buf_dec.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: trim
  30. |Callby: command
  31. |Purpose: Trims trailing blanks from the select range or the entire file.
  32. |Arguments:
  33. |    none
  34. \******************************************************************************/
  35. void trim()
  36. {
  37.     register Int byt;
  38.     Int offset;
  39.     register Char *a;
  40.     register rec_ptr r1,r2;
  41.  
  42.     if(!SELREC)    /* do the whole file */
  43.     {
  44.         r2 = WINDOW[CURWINDOW].base;
  45.         r1 = r2->next;
  46.     }
  47.     else
  48.     {
  49.         offset = get_offset(SELREC,SELBYT,get_seldir());
  50.         if(!offset)
  51.         {
  52.             r1 = CURREC;
  53.             r2 = r1->next;
  54.         }
  55.         else if(offset < 0)    /* select marker preceeds current position */
  56.         {
  57.             r1 = CURREC;    /* find the other end of the buffer, backwards */
  58.             byt = CURBYT;
  59.             while(offset++ < 0)
  60.             {
  61.                 if(!byt)
  62.                 {
  63.                     if(r1->prev == BASE)
  64.                         break;
  65.                     r1 = r1->prev;
  66.                     byt = r1->length;
  67.                 }
  68.                 else
  69.                     byt--;
  70.             }
  71.             r2 = CURREC->next;
  72.         }
  73.         else
  74.         {
  75.             
  76.             r1 = r2 = CURREC;    /* find the other end of the buffer, forwards */
  77.             byt = CURBYT;
  78.             while(offset-- > 0)
  79.             {
  80.                 if(r2 == BASE)
  81.                     break;
  82.                 if(byt == r2->length)
  83.                 {
  84.                     r2 = r2->next;
  85.                     byt = 0;
  86.                 }
  87.                 else
  88.                     byt++;
  89.             }
  90.             r2 = r2->next;
  91.         }
  92.         unselect();
  93.     }
  94.     while(r1 != r2)
  95.     {
  96.         if(r1->length)
  97.         {
  98.             a = r1->data + r1->length;
  99.             while(isspace(*--a))
  100.                 if(a < r1->data)
  101.                     break;
  102.             r1->length = ++a - r1->data;
  103.             *(r1->data + r1->length) = '\0';
  104.         }
  105.         r1 = r1->next;
  106.     }
  107.     if(CURBYT > CURREC->length)
  108.         CURBYT = CURREC->length;
  109.     CURCOL = get_column(CURREC,CURBYT);
  110.     move(CURROW,CURCOL);
  111. }
  112.  
  113.